﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TalentRecruitmentSystem.Service
{
    public class UserInfoService
    {
        Entities entity = new Entities();
        /// <summary>
        /// 添加新用户
        /// </summary>
        /// <param name="userinfo"></param>
        /// <returns></returns>
        public bool add(UserInfo userinfo)
        {
            entity.UserInfo.AddObject(userinfo);
            if (entity.SaveChanges() > 0)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 根据用户id获取用户信息
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public UserInfo getUserInfoByUserID(int userid)
        {
            return entity.UserInfo.Single(u => u.UserID == userid);
        }
        /// <summary>
        /// 根据用户名获取用户信息
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public UserInfo getUserInfoByUserName(string username)
        {
            UserInfo userInfo = new UserInfo();
            bool b = entity.UserInfo.Any(u => u.UserName == username);
            if (b == true)
            {
                return entity.UserInfo.Single(u => u.UserName == username);
            }
            else
                return null;
        }
        /// <summary>
        /// 改密码
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool UpdatePSW(string username, string password)
        {
            UserInfo userinfo = getUserInfoByUserName(username);
            userinfo.Password = password;
            entity.UserInfo.ApplyCurrentValues(userinfo);
            if (entity.SaveChanges() > 0)
            {
                return true;
            }
            return false;
        }
        public bool updateUserInfo(UserInfo userInfo)
        {
            if (entity.SaveChanges() > 0)
            {
                entity.UserInfo.ApplyCurrentValues(userInfo);
                return true;
            }
            return false;
        }
        public List<UserInfo> getUserInfosByParameter(string position, string workpalce, string education)
        {
            return entity.UserInfo.Where(u => u.Expectpositon == position & u.Workplace == workpalce & u.Education == education).ToList();
        }
    }
}